<?php
namespace app\admin\controller;
use think\Request;
use think\Controller;

class Base extends Controller
{
    /**
     * @var \think\Request Request实例
     */
    protected $request;
    protected $tableName;
    
    /**
     * 构造方法
     * @param Request $request Request对象
     * @access public
     */
    public function __construct(Request $request)
    {
		  $this->request = $request;
    }
    /**
     * 暂时列表功能
     */
    public function index()
    {
      return json(Db($this->tableName)->select());
    }
    /**
     * 编辑功能
     * 2019-09-12 17:07
     */
    public function edit()
    {
        $data = $this->request->param();
        $id = $this->request->param($this->priKey);
        if(empty($id)){
            $id = \Db::name($this->tableName)->insertGetId($data);
        }else{
            $id = \Db::name($this->tableName)->where($this->priKey, $id)->update($data);
        }
        return json(['success'=>true,'id'=>$id]);
    }
    /**
     * 删除功能
     * 2019-09-12 17:07
     */
    public function delete()
    {
        $ret = array('code'=>1,'msg'=>'未提交参数');
        $ids = $this->request->param('ids');
        if(empty($ids)){
            return json($ret);
        }
        $ids = \json_decode($ids);
        if(empty($this->tableName)){
          exit('tableName not exists');
        }
        $ret['code'] = 0 ;
        $ret['count'] = \Db::name($this->tableName)->delete($ids);
        return json($ret);
    }
}
